﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Identity;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Organization;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.StaffApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.StaffAPI
{
    [Description("API-账户")]
    [ApiAuthentication]
    public class StaffAccountController : BaseApiController
    {
        public IStaffLoginContract StaffLoginContract { set; get; }
        public IStaffContract StaffContract { get; set; }

        public IStaffRolesContract StaffRolesContract { get; set; }
        public IRoleAuthorityContract RoleAuthorityContract { get; set; }
        public IStaffAuthorityContract StaffAuthorityContract { get; set; }


        [Description("API-账户-登录")]
        [AllowAnonymous]
        public IHttpActionResult Login([FromBody]StaffLoginParamsModel entity)
        {

            if (entity == null)
            {
                var result = new AjaxResult("传输参数无效", AjaxResultType.Error);
                return Token(result, "");
            }

            var staffLoginModel = BaseWebApiExt.StaffApiLogin(StaffLoginContract, entity.LoginName, entity.LoginPass);

            if (staffLoginModel != null && staffLoginModel.Id != 0)
            {

                var staffModel = StaffContract.GetByKey(staffLoginModel.StaffID);



                List<int> l = (from a in StaffRolesContract.Entities where a.StaffID == staffLoginModel.StaffID select a.RoleID).ToList();
                List<int> al = new List<int>();
                var qa = from a in RoleAuthorityContract.Entities
                         join
                             b in StaffRolesContract.Entities on a.RoleID equals b.RoleID
                         where b.StaffID == staffModel.Id
                         select a.AuthorityID;
                al.AddRange(qa.ToList());
                var sa = from a in StaffAuthorityContract.Entities
                         where a.StaffID == staffModel.Id
                         select a.AuthorityID;
                al.AddRange(sa.ToList());


                var result = new AjaxResult("登录成功", AjaxResultType.Success, new { Id = staffLoginModel.Id, LoginName = staffLoginModel.LoginName, Name = staffModel.Name, Roles = l, Authoritys=al });

                staffLoginModel.Token = BaseWebApiExt.StaffGetNewToken(StaffLoginContract, staffLoginModel.Id);//获取新的toekn

                return Token(result, BaseWebApiExt.ToCiphertextToken(staffLoginModel.Token));
            }
            else
            {
                var result = new AjaxResult("用户名或密码错误", AjaxResultType.Error, new { });
                return Token(result, "");
            }
        }

        [Description("API-账户-修改密码")]
        public IHttpActionResult UpdateLoginPass([FromBody]StaffLoginParamsModel entity)
        {
            if (entity == null)
            {
                var result = new AjaxResult("传输参数无效", AjaxResultType.Error);
                return Token(result, "");
            }

            var userData = StaffLoginContract.GetByKey(entity.Id);

            if (userData == null)
            {
                var result = new AjaxResult("没有找到用户", AjaxResultType.Error);
                return Token(result, "");
            }
            if (userData.LoginPass != entity.OldPassWord)
            {
                var result = new AjaxResult("原密码错误", AjaxResultType.Error);
                return Token(result, "");
            }
            else if (entity.OldPassWord == entity.ResPassWord)
            {
                var result = new AjaxResult("两次密码输入不一致", AjaxResultType.Error);
                return Token(result, "");
            }
            else
            {
                StaffLoginContract.UpdateDirect(d => userData.Id == d.Id, d => new StaffLogin { LoginPass = entity.LoginPass });

                var result = new AjaxResult("成功", AjaxResultType.Success, userData);
                return Token(result, "tommyheng123");
            }
        }
    }
}
